#include "stdafx.h"
#include <locale.h>
#include <stdio.h>
#include "string_converters.h"
namespace Utility {
    
std::wstring to_wstring(const char* p) {
  if(!p || *p == 0)
    return std::wstring() ;
  size_t len = strlen(p) ;
  wchar_t* new_ws = new wchar_t[len * 2] ; // bug fix
  setlocale(LC_ALL, "chs") ;
  _snwprintf(new_ws, len*2, L"%S", p) ;
  std::wstring wstr(new_ws) ;
  delete[] new_ws ;
  return wstr ;
}

std::string to_string(const std::wstring& wstr) {
  if(wstr.empty())
    return std::string() ;
  char* new_s = new char[wstr.size() * 3] ; // safe but maybe has bug.?!
  setlocale(LC_ALL,"chs") ;
  _snprintf(new_s, wstr.size() * 3, "%S", wstr.c_str()) ;
  std::string str(new_s) ;
  delete[] new_s ;
  return str ;
}

std::wstring to_wstring(const std::string& str) {
  return to_wstring(str.c_str()) ;
}
}
